﻿Public Partial Class BulkVessel_Month
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "PORT BULK BARGE VESSEL "
            ViewState("Bas_Vessel") = CreateDataTabelBas()
            ViewState("Edit_Bas") = ""
            'ทำการ Load ข้อมูล เรือ Berge
            LoadData("", "", "", "0", "")
        End If
    End Sub

    Private Sub LoadData(ByVal BV_no As String, ByVal Company As String, ByVal Product_Name As String, ByVal Wharf As String, ByVal Fender As String)
        ViewState("Bulk") = clsGet.Get_Booking_Bas_Vessel(BV_no, Company, Product_Name, Wharf, Fender)
        PanelVesselBulk.Visible = True
        PanelAddVessel.Visible = False
        gridItem.PageIndex = 0
        gridItem.DataSource = ViewState("Bulk")
        gridItem.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("Bulk")
        gridItem.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("Bulk")
        gridItem.DataBind()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        'ทำการโหลดข้อมูลใหม่กรณีทำการปิด Popup จะมาทำการ Load ใหม่เสมอ ยกเว้นการ ยกเลิก
        LoadData("", "", "", "0", "")
    End Sub

    Private Sub gridItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridItem.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridItem.PageIndex <> 0) Then
            Index = Index Mod (gridItem.PageIndex * 10)
        End If
        If (e.CommandName = "Edit") Then
            'เข้ามาทำการเพิ่ม ข้อมูล หรือ ตรวจสอบข้อมูล หรือ อนุมัติข้อมูล

            PanelVesselBulk.Visible = False
            PanelAddVessel.Visible = True
            If (gridItem.DataKeys(Index).Values("Complete") & "" = "Y") Then
                btnSave.Visible = False
                btnComplete.Visible = False
                btnAddBasVessel.Visible = False
                btnSend.Visible = False
                btnCheck.Visible = False
                btnReject.Visible = False
            ElseIf (gridItem.DataKeys(Index).Values("Complete") & "" = "S") Then
                'Waiting For Check
                If (Session("Role") = "00016") Or (Session("Role") = "00015") Then
                    btnSave.Visible = True
                    btnComplete.Visible = False
                    btnSend.Visible = False
                    btnCheck.Visible = True
                    btnAddBasVessel.Visible = True
                    btnReject.Visible = True
                Else
                    btnSave.Visible = False
                    btnComplete.Visible = False
                    btnSend.Visible = False
                    btnCheck.Visible = False
                    btnAddBasVessel.Visible = False
                    btnReject.Visible = False
                End If

            ElseIf (gridItem.DataKeys(Index).Values("Complete") & "" = "C") Then
                'Waiting For Approve
                If (Session("Role") = "00016") Then
                    btnSave.Visible = True
                    btnComplete.Visible = True
                    btnSend.Visible = False
                    btnCheck.Visible = False
                    btnAddBasVessel.Visible = True
                    btnReject.Visible = True
                Else
                    btnSave.Visible = False
                    btnComplete.Visible = False
                    btnSend.Visible = False
                    btnCheck.Visible = False
                    btnAddBasVessel.Visible = False
                    btnReject.Visible = False
                End If

            Else
                btnSave.Visible = True
                btnReject.Visible = False
                btnComplete.Visible = False
                btnSend.Visible = True
                btnCheck.Visible = False
                btnAddBasVessel.Visible = True
            End If
            If (gridItem.DataKeys(Index).Values("Send_By") & "" <> "") Then
                rowSend.Visible = True
                lbSend.Text = gridItem.DataKeys(Index).Values("Send_By") & ""
            Else
                rowSend.Visible = False
            End If
            If (gridItem.DataKeys(Index).Values("Check_By") & "" <> "") Then
                rowCheck.Visible = True
                lbCheck.Text = gridItem.DataKeys(Index).Values("Check_By") & ""
            Else
                rowCheck.Visible = False
            End If
            If (gridItem.DataKeys(Index).Values("Approve_By") & "" <> "") Then
                rowApprove.Visible = True
                lbApprove.Text = gridItem.DataKeys(Index).Values("Approve_By") & ""
            Else
                rowApprove.Visible = False
            End If
            If (gridItem.DataKeys(Index).Values("Reject_By") & "" <> "") Then
                rowReject.Visible = True
                rowRejectDescription.Visible = True
                lbReject.Text = gridItem.DataKeys(Index).Values("Reject_By") & ""
                lbRejectDescription.Text = gridItem.DataKeys(Index).Values("Reject_Description") & ""
            Else
                rowReject.Visible = False
                rowRejectDescription.Visible = False
            End If

            ViewState("Complete") = gridItem.DataKeys(Index).Values("Complete")
            ViewState("BV_no") = gridItem.DataKeys(Index).Values("BV_no")
            ViewState("Bas_Vessel") = clsGet.Get_Bas_Vessel_In(ViewState("BV_no"))
            Dim ds As DataTable = ViewState("Bas_Vessel")
            ds.Columns.Add("Date_Sort", GetType(String))
            ds.AcceptChanges()
            For i As Integer = 0 To ds.Rows.Count - 1
                ds.Rows(i).Item("Date_Sort") = CDate(ds.Rows(i).Item("StartDate")).ToString("yyyyMMdd") & ds.Rows(i).Item("StartTime").ToString().Split(":")(0) & ds.Rows(i).Item("StartTime").ToString().Split(":")(1)
                ds.AcceptChanges()
            Next
            Dim dsView As DataView = ds.DefaultView
            dsView.Sort = " Date_Sort"
            ds = New DataTable
            ds = clsGet.ConvertDataViewToDataSet(dsView).Copy
            ViewState("Bas_Vessel") = ds
            gridVessel.DataSource = ViewState("Bas_Vessel")
            gridVessel.DataBind()

            lbCompanyHead.Text = "บริษัท : " & gridItem.DataKeys(Index).Values("Company") & ""
            lbProductHead.Text = "สินค้า : " & gridItem.DataKeys(Index).Values("Product_name") & ""
            lbwharfHead.Text = "ท่าเรือ : " & gridItem.DataKeys(Index).Values("WharfShow") & ",Fender : " & gridItem.DataKeys(Index).Values("Fender")
        ElseIf e.CommandName = "cmdedit" Then
            'Pop up หน้าทำการ Edit เรือ Berge
            Session("Edit_Bas") = gridItem.DataKeys(Index).Values("BV_no") & "$" & gridItem.DataKeys(Index).Values("Company") & "$" & gridItem.DataKeys(Index).Values("Product_name") & "$" & gridItem.DataKeys(Index).Values("Wharf") & "$" & gridItem.DataKeys(Index).Values("Fender")
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "cmddelete" Then
            'ทำการลบข้อมูลเรือ Berge โดยทำการลบทั้งข้อมูลเรือ Berge และรายละเอียดเรือที่เข้าใช้งานท่าเรือ(เรือ Berge)
            Dim Line_Code As String = gridItem.DataKeys(Index).Values("BV_no")
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteBooking_Bas_Vessel(Line_Code, conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadData("", "", "", "0", "")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim dt As DataTable = clsGet.Get_Bas_Vessel_In(gridItem.DataKeys(e.Row.RowIndex).Values("BV_no") & "")
            Dim SumTruck As String = "0"
            Dim SumProduct As String = "0"
            Dim Dateshow As String = ""
            For Each dr As DataRow In dt.Rows
                Try
                    SumTruck = CDbl(SumTruck) + CDbl(dr.Item("Truck6")) + CDbl(dr.Item("Truck810")) + CDbl(dr.Item("Trailer")) + CDbl(dr.Item("Trailer2"))
                    SumProduct = CDbl(SumProduct) + CDbl(dr.Item("Product_Amount"))
                Catch ex As Exception

                End Try
            Next
            If (dt.Rows.Count > 0) Then
                dt.Columns.Add("Date_Sort", GetType(String))
                dt.AcceptChanges()
                For i As Integer = 0 To dt.Rows.Count - 1
                    dt.Rows(i).Item("Date_Sort") = CDate(dt.Rows(i).Item("StartDate")).ToString("yyyyMMdd") & dt.Rows(i).Item("StartTime").ToString().Split(":")(0) & dt.Rows(i).Item("StartTime").ToString().Split(":")(1)
                    dt.AcceptChanges()
                Next
                Dim dsView As DataView = dt.DefaultView
                dsView.Sort = " Date_Sort"
                dt = New DataTable
                dt = clsGet.ConvertDataViewToDataSet(dsView).Copy
                If (dt.Rows(0).Item("StartDate") & "" = dt.Rows(dt.Rows.Count - 1).Item("EndDate")) Then
                    Dateshow = dt.Rows(dt.Rows.Count - 1).Item("EndDate")
                Else
                    Dateshow = dt.Rows(0).Item("StartDate").ToString.Substring(0, 2) & "-" & dt.Rows(dt.Rows.Count - 1).Item("EndDate")
                End If
            End If
            e.Row.Cells(5).Text = Dateshow
            e.Row.Cells(6).Text = FormatNumber(SumTruck, 0)
            e.Row.Cells(7).Text = FormatNumber(SumProduct)
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridItem.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridItem.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridItem.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridItem.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridItem_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridItem.RowEditing

    End Sub

    Private Sub BtnNewItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNewItem.Click
        Session("Edit_Bas") = Nothing
        ModalPopupExtender1.Show()
    End Sub

    Private Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        LoadData("", "", "", "0", "")
    End Sub

    Private Function CreateDataTabelBas() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Bas_no", GetType(String))
        dt.Columns.Add("Bas_name", GetType(String))
        dt.Columns.Add("StartDate", GetType(String))
        dt.Columns.Add("StartTime", GetType(String))
        dt.Columns.Add("StartShow", GetType(String))
        dt.Columns.Add("EndDate", GetType(String))
        dt.Columns.Add("EndTime", GetType(String))
        dt.Columns.Add("EndShow", GetType(String))
        dt.Columns.Add("Truck6", GetType(String))
        dt.Columns.Add("Truck810", GetType(String))
        dt.Columns.Add("Trailer", GetType(String))
        dt.Columns.Add("Trailer2", GetType(String))
        dt.Columns.Add("Product_Amount", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Sub btnAddBasVessel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddBasVessel.Click
        Dim Bas_no As String = clsGet.Get_Basno(txtVessel.Text)
        If (txtVessel.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Bas Vessel');", True)
            Exit Sub
        ElseIf (Bas_no = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบข้อมูล Bas Vessel');", True)
            Exit Sub
        End If
        If (txtStartDate.Text.Trim() = "") Or (txtStartTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ วัน/เวลา เริ่มการขนถ่าย');", True)
            Exit Sub
        End If
        If (txtEndDate.Text.Trim() = "") Or (txtEndTime.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ วัน/เวลา เสร็จการขนถ่าย');", True)
            Exit Sub
        End If
        If (txtTruck6.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวนรถยนต์บรรทุกไม่เกิน 6 ล้อ ');", True)
            Exit Sub
        End If
        If (txtTruck810.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวนรถยนต์บรรทุก 8 หรือ 10 ล้อ');", True)
            Exit Sub
        End If
        If (txtTrailer.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวนรถพ่วง');", True)
            Exit Sub
        End If
        If (txtTrailer2.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุจำนวนรถพ่วงมากกว่า 2 axle');", True)
            Exit Sub
        End If
        If (txtProduct.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุประมาณน้ำหนัก');", True)
            Exit Sub
        End If
        Dim dt As DataTable = ViewState("Bas_Vessel")
        If ViewState("Edit_Bas") & "" = "" Then
            'ใหม่
            For Each dk As DataRow In dt.Rows
                If (dk.Item("StartDate") = txtStartDate.Text) And (dk.Item("Bas_no") = Bas_no) Then
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('พบข้อมูลซ้ำ');", True)
                    Exit Sub
                End If
            Next
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Bas_no") = Bas_no
            dr.Item("Bas_name") = txtVessel.Text
            dr.Item("StartDate") = txtStartDate.Text
            dr.Item("StartTime") = txtStartTime.Text
            dr.Item("StartShow") = txtStartDate.Text & " - " & txtStartTime.Text
            dr.Item("EndDate") = txtEndDate.Text
            dr.Item("EndTime") = txtEndTime.Text
            dr.Item("EndShow") = txtEndDate.Text & " - " & txtEndTime.Text
            dr.Item("Truck6") = txtTruck6.Text
            dr.Item("Truck810") = txtTruck810.Text
            dr.Item("Trailer") = txtTrailer.Text
            dr.Item("Trailer2") = txtTrailer2.Text
            dr.Item("Product_Amount") = txtProduct.Text
            dt.Rows.Add(dr)

        Else
            'เก่า
            Dim Index As Integer = CInt(ViewState("Edit_Bas"))
            dt.Rows(Index).Item("Bas_no") = Bas_no
            dt.Rows(Index).Item("Bas_name") = txtVessel.Text
            dt.Rows(Index).Item("StartDate") = txtStartDate.Text
            dt.Rows(Index).Item("StartTime") = txtStartTime.Text
            dt.Rows(Index).Item("StartShow") = txtStartDate.Text & " - " & txtStartTime.Text
            dt.Rows(Index).Item("EndDate") = txtEndDate.Text
            dt.Rows(Index).Item("EndTime") = txtEndTime.Text
            dt.Rows(Index).Item("EndShow") = txtEndDate.Text & " - " & txtEndTime.Text
            dt.Rows(Index).Item("Truck6") = txtTruck6.Text
            dt.Rows(Index).Item("Truck810") = txtTruck810.Text
            dt.Rows(Index).Item("Trailer") = txtTrailer.Text
            dt.Rows(Index).Item("Trailer2") = txtTrailer2.Text
            dt.Rows(Index).Item("Product_Amount") = txtProduct.Text

        End If
        dt.AcceptChanges()
        ViewState("Bas_Vessel") = dt
        ClearBasVessel()
        gridVessel.DataSource = dt
        gridVessel.DataBind()
    End Sub

    Private Sub ClearBasVessel()
        txtVessel.Text = String.Empty
        txtStartDate.Text = String.Empty
        txtStartTime.Text = String.Empty
        txtEndDate.Text = String.Empty
        txtEndTime.Text = String.Empty
        txtTruck6.Text = "0"
        txtTruck810.Text = "0"
        txtTrailer.Text = "0"
        txtTrailer2.Text = "0"
        txtProduct.Text = "0"
        ViewState("Edit_Bas") = ""
    End Sub

    Private Sub gridVessel_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridVessel.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridItem.PageIndex <> 0) Then
            Index = Index Mod (gridItem.PageIndex * 10)
        End If
        Dim dt As DataTable = ViewState("Bas_Vessel")
        If e.CommandName = "cmdedit" Then
            ViewState("Edit_Bas") = Index

            txtVessel.Text = dt.Rows(Index).Item("Bas_name")
            txtStartDate.Text = dt.Rows(Index).Item("StartDate")
            txtStartTime.Text = dt.Rows(Index).Item("StartTime")
            txtEndDate.Text = dt.Rows(Index).Item("EndDate")
            txtEndTime.Text = dt.Rows(Index).Item("EndTime")
            txtTruck6.Text = dt.Rows(Index).Item("Truck6")
            txtTruck810.Text = dt.Rows(Index).Item("Truck810")
            txtTrailer.Text = dt.Rows(Index).Item("Trailer")
            txtTrailer2.Text = dt.Rows(Index).Item("Trailer2")
            txtProduct.Text = dt.Rows(Index).Item("Product_Amount")
        ElseIf e.CommandName = "cmddelete" Then
            dt.Rows(Index).Delete()
            dt.AcceptChanges()
            ViewState("Bas_Vessel") = dt
            gridVessel.DataSource = dt
            gridVessel.DataBind()
        End If
    End Sub

    Private Sub gridVessel_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridVessel.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim ts = DateDiff("n", gridVessel.DataKeys(e.Row.RowIndex).Values("StartDate") & " " & gridVessel.DataKeys(e.Row.RowIndex).Values("StartTime"), gridVessel.DataKeys(e.Row.RowIndex).Values("EndDate") & " " & gridVessel.DataKeys(e.Row.RowIndex).Values("EndTime")) ' in case you need it in minutes

            e.Row.Cells(4).Text = (ts \ 60).ToString("00") & " ชั่วโมง " & (ts Mod 60).ToString("00") & " นาที"

            e.Row.Cells(5).Text = FormatNumber(e.Row.Cells(5).Text, 0)
            e.Row.Cells(6).Text = FormatNumber(e.Row.Cells(6).Text, 0)
            e.Row.Cells(7).Text = FormatNumber(e.Row.Cells(7).Text, 0)
            e.Row.Cells(8).Text = FormatNumber(e.Row.Cells(8).Text, 0)
            e.Row.Cells(9).Text = FormatNumber(e.Row.Cells(9).Text)
        ElseIf (e.Row.RowType = DataControlRowType.Footer) Then
            Dim truck6 As Integer = 0
            Dim truck810 As Integer = 0
            Dim Trailer As Integer = 0
            Dim Trailer2 As Integer = 0
            Dim SumMT As Double = 0.0
            For i = 0 To gridVessel.Rows.Count - 1
                Try
                    truck6 += CInt(gridVessel.Rows(i).Cells(5).Text)
                    truck810 += CInt(gridVessel.Rows(i).Cells(6).Text)
                    Trailer += CInt(gridVessel.Rows(i).Cells(7).Text)
                    Trailer2 += CInt(gridVessel.Rows(i).Cells(8).Text)
                    SumMT += CDbl(gridVessel.Rows(i).Cells(9).Text)
                Catch ex As Exception

                End Try
            Next
            e.Row.Cells(5).Text = FormatNumber(truck6, 0)
            e.Row.Cells(6).Text = FormatNumber(truck810, 0)
            e.Row.Cells(7).Text = FormatNumber(Trailer, 0)
            e.Row.Cells(8).Text = FormatNumber(Trailer2, 0)
            e.Row.Cells(9).Text = FormatNumber(SumMT)
            ViewState("SUMMT") = FormatNumber(SumMT)
            e.Row.BackColor = Drawing.Color.Yellow
        End If
    End Sub

    Private Sub gridVessel_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridVessel.RowEditing
       
    End Sub

    Private Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim Approve As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name") & " วันที่ : " & DateTime.Now.ToString("dd MMMM yyyy HH:mm")
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateBooking_Bas_Vessel(ViewState("BV_no"), "Y", "", "", Approve, "", "", conn, tran) > 0) Then
            ViewState("BV_no") = ""
           LoadData("", "", "", "0", "")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()

        tran = clsDB.BeginTran(conn)
        Dim dt As DataTable = ViewState("Bas_Vessel")
        Dim Index As Integer = 1
        clsFc.DeleteBas_Vessel_In(ViewState("BV_no"), conn, tran)
        For Each dr As DataRow In dt.Rows
            If (clsFc.InsertBas_Vessel_In(ViewState("BV_no"), Index, dr.Item("Bas_no"), dr.Item("StartDate"), dr.Item("StartTime"), dr.Item("EndDate"), dr.Item("EndTime"), dr.Item("Truck6"), dr.Item("Truck810"), dr.Item("Trailer"), dr.Item("Trailer2"), dr.Item("Product_Amount"), conn, tran) < 1) Then
                condition = False
                Exit For
            Else
                Index += 1
            End If
        Next
        If (condition) Then
            clsDB.CommitTran(tran)
            ViewState("BV_no") = ""
            LoadData("", "", "", "0", "")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลเรียบร้อย');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If

        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnCheck_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim Approve As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name") & " วันที่ : " & DateTime.Now.ToString("dd MMMM yyyy HH:mm")
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateBooking_Bas_Vessel(ViewState("BV_no"), "C", "", Approve, "", "", "", conn, tran) > 0) Then
            ViewState("BV_no") = ""
            LoadData("", "", "", "0", "")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim Approve As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("Name") & " วันที่ : " & DateTime.Now.ToString("dd MMMM yyyy HH:mm")
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateBooking_Bas_Vessel(ViewState("BV_no"), "S", Approve, "", "", "", "", conn, tran) > 0) Then
            ViewState("BV_no") = ""
            LoadData("", "", "", "0", "")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub btnReject_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReject.Click
        ModalPopupExtender6.Show()
    End Sub

    Private Sub btnRejectOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRejectOK.Click
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim Approve As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name") & " วันที่ : " & DateTime.Now.ToString("dd MMMM yyyy HH:mm")
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim Status As String = ""
        If ViewState("Complete") & "" = "C" Then
            Status = "S"
        ElseIf ViewState("Complete") & "" = "S" Then
            Status = "N"
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('พบข้อมูลบางส่วนขาดหายไปกรุณาทำการ  ออกจากระบบ และทำการเข้าใหม่อีกครั้ง !!');", True)
        End If
        Application.Lock()
        conn.Open()

        '  tran = clsDB.BeginTran(conn)
        If (clsFc.UpdateBooking_Bas_Vessel(ViewState("BV_no"), Status, "", "", "", Approve, Session("Cancel_Description") & "", conn, tran) > 0) Then
            ViewState("BV_no") = ""
            Session("Cancel_Description") = Nothing
            LoadData("", "", "", "0", "")
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
        End If
        conn.Close()
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        dsReport.Tables.Add(Get_Value())
        dsReport.AcceptChanges()

        ReportName = "Berge_Report.rdlc"
        ' dsReport = New DataSet
        reportDataSource = "Berge_Value"

        paraName = "Company;Product;Wharf;Booking_no;BergeCount;DateFrom;DateTo;MT"
        paraValue = lbCompanyHead.Text & ";" & lbProductHead.Text & ";" & lbwharfHead.Text & ";B" & ViewState("BV_no") & ";" & dsReport.Tables(0).Rows.Count & ";" & dsReport.Tables(0).Rows(0).Item("From").ToString.Split("-")(0) & ";" & dsReport.Tables(0).Rows(dsReport.Tables(0).Rows.Count - 1).Item("To").ToString.Split("-")(0) & ";" & ViewState("SUMMT")

        Orientation = True
        reportType = "pdf"

        urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
    End Sub

    Private Function Get_Value() As DataTable
        Dim ds As DataTable = clsGet.Get_Bas_Vessel_In(ViewState("BV_no"))
        ds.Columns.Add("Date_Sort", GetType(String))
        ds.AcceptChanges()
        For i As Integer = 0 To ds.Rows.Count - 1
            ds.Rows(i).Item("Date_Sort") = CDate(ds.Rows(i).Item("StartDate")).ToString("yyyyMMdd") & ds.Rows(i).Item("StartTime").ToString().Split(":")(0) & ds.Rows(i).Item("StartTime").ToString().Split(":")(1)
            ds.AcceptChanges()
        Next
        Dim dsView As DataView = ds.DefaultView
        dsView.Sort = " Date_Sort"
        ds = New DataTable
        ds = clsGet.ConvertDataViewToDataSet(dsView).Copy
        Dim dt As DataTable = Create()
        Dim count As Integer = 1
        For Each dr As DataRow In ds.Rows
            Dim dn As DataRow = dt.NewRow()
            dn.Item("No.") = count
            dn.Item("Name") = dr.Item("Bas_name") & ""
            dn.Item("From") = dr.Item("StartShow") & ""
            dn.Item("To") = dr.Item("EndShow") & ""
            Dim ts = DateDiff("n", dr.Item("StartDate") & " " & dr.Item("StartTime"), dr.Item("EndDate") & " " & dr.Item("EndTime")) ' in case you need it in minutes

            dn.Item("Hrs") = (ts \ 60).ToString("00") & " ชั่วโมง " & (ts Mod 60).ToString("00") & " นาที"
            dn.Item("Truck6") = dr.Item("Truck6") & ""
            dn.Item("Truck810") = dr.Item("Truck810") & ""
            dn.Item("Trailer") = dr.Item("Trailer") & ""
            dn.Item("Trailer2") = dr.Item("Trailer2") & ""
            dn.Item("MT") = dr.Item("Product_Amount") & ""
            dt.Rows.Add(dn)
            dt.AcceptChanges()
            count += 1
        Next
        Return dt
    End Function

    Private Function Create() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("No.", GetType(String))
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("From", GetType(String))
        dt.Columns.Add("To", GetType(String))
        dt.Columns.Add("Hrs", GetType(String))
        dt.Columns.Add("Truck6", GetType(String))
        dt.Columns.Add("Truck810", GetType(String))
        dt.Columns.Add("Trailer", GetType(String))
        dt.Columns.Add("Trailer2", GetType(String))
        dt.Columns.Add("MT", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function
End Class